// This is mutant program.
// Author : ysma

package edu.towson.cosc603.coffeemaker;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main
{

    private static edu.towson.cosc603.coffeemaker.CoffeeMaker coffeeMaker;

    public static void mainMenu()
    {
        System.out.println( "1. Add a recipe" );
        System.out.println( "2. Delete a recipe" );
        System.out.println( "3. Edit a recipe" );
        System.out.println( "4. Add inventory" );
        System.out.println( "5. Check inventory" );
        System.out.println( "6. Make coffee" );
        System.out.println( "0. Exit\n" );
        int userInput = stringToInt( inputOutput( "Please press the number that corresponds to what you would like the coffee maker to do." ) );
        if (userInput == 1) {
            addRecipe();
        }
        if (userInput == 2) {
            deleteRecipe();
        }
        if (userInput == 3) {
            editRecipe();
        }
        if (userInput == 4) {
            addInventory();
        }
        if (userInput == 5) {
            checkInventory();
        }
        if (userInput == 6) {
            makeCoffee();
        }
        if (userInput == 0) {
            System.exit( 0 );
        }
    }

    public static void addRecipe()
    {
        java.lang.String name = inputOutput( "\nPlease enter the recipe name: " );
        java.lang.String priceString = inputOutput( "\nPlease enter the recipe price: $" );
        int price = stringToInt( priceString );
        if (price < 0) {
            mainMenu();
        }
        java.lang.String coffeeString = inputOutput( "\nPlease enter the units of coffee in the recipe: " );
        int amtCoffee = stringToInt( coffeeString );
        if (amtCoffee < 0) {
            mainMenu();
        }
        java.lang.String milkString = inputOutput( "\nPlease enter the units of milk in the recipe: " );
        int amtMilk = stringToInt( milkString );
        if (amtMilk < 0) {
            mainMenu();
        }
        java.lang.String sugarString = inputOutput( "\nPlease enter the units of sugar in the recipe: " );
        int amtSugar = stringToInt( sugarString );
        if (amtSugar < 0) {
            mainMenu();
        }
        java.lang.String chocolateString = inputOutput( "\nPlease enter the units of chocolate in the recipe: " );
        int amtChocolate = stringToInt( chocolateString );
        if (amtChocolate < 0) {
            mainMenu();
        }
        boolean recipeAdded = false;
        edu.towson.cosc603.coffeemaker.Recipe r = new edu.towson.cosc603.coffeemaker.Recipe();
        r.setName( name );
        r.setPrice( price );
        r.setAmtCoffee( amtCoffee );
        r.setAmtMilk( amtMilk );
        r.setAmtSugar( amtSugar );
        r.setAmtChocolate( amtChocolate );
        recipeAdded = coffeeMaker.addRecipe( r );
        if (recipeAdded) {
            System.out.println( name + " successfully added." );
        } else {
            System.out.println( name + "could not be added." );
        }
        mainMenu();
    }

    public static void deleteRecipe()
    {
        edu.towson.cosc603.coffeemaker.Recipe[] recipes = coffeeMaker.getRecipes();
        for (int i = 0; i < recipes.length; i++) {
            System.out.println( i + 1 + ". " + recipes[i].getName() );
        }
        java.lang.String recipeToDeleteString = inputOutput( "Please select the number of the recipe to delete." );
        int recipeToDelete = stringToInt( recipeToDeleteString ) - 1;
        if (recipeToDelete < 0) {
            mainMenu();
        }
        boolean recipeDeleted = coffeeMaker.deleteRecipe( recipes[recipeToDelete] );
        if (recipeDeleted) {
            System.out.println( recipes[recipeToDelete].getName() + " successfully deleted." );
        } else {
            System.out.println( recipes[recipeToDelete].getName() + "could not be deleted." );
        }
        mainMenu();
    }

    public static void editRecipe()
    {
        edu.towson.cosc603.coffeemaker.Recipe[] recipes = coffeeMaker.getRecipes();
        for (int i = 0; i < recipes.length; i++) {
            System.out.println( i + 1 + ". " + recipes[i].getName() );
        }
        java.lang.String recipeToEditString = inputOutput( "Please select the number of the recipe to edit." );
        int recipeToEdit = stringToInt( recipeToEditString ) - 1;
        if (recipeToEdit < 0) {
            mainMenu();
        }
        edu.towson.cosc603.coffeemaker.Recipe oldRecipe = recipes[recipeToEdit];
        java.lang.String name = inputOutput( "\nPlease enter the recipe name: " );
        java.lang.String priceString = inputOutput( "\nPlease enter the recipe price: $" );
        int price = stringToInt( priceString );
        if (price < 0) {
            mainMenu();
        }
        java.lang.String coffeeString = inputOutput( "\nPlease enter the units of coffee in the recipe: " );
        int amtCoffee = stringToInt( coffeeString );
        if (amtCoffee < 0) {
            mainMenu();
        }
        java.lang.String milkString = inputOutput( "\nPlease enter the units of milk in the recipe: " );
        int amtMilk = stringToInt( milkString );
        if (amtMilk < 0) {
            mainMenu();
        }
        java.lang.String sugarString = inputOutput( "\nPlease enter the units of sugar in the recipe: " );
        int amtSugar = stringToInt( sugarString );
        if (amtSugar < 0) {
            mainMenu();
        }
        java.lang.String chocolateString = inputOutput( "\nPlease enter the units of chocolate in the recipe: " );
        int amtChocolate = stringToInt( chocolateString );
        if (amtChocolate < 0) {
            mainMenu();
        }
        edu.towson.cosc603.coffeemaker.Recipe newRecipe = new edu.towson.cosc603.coffeemaker.Recipe();
        newRecipe.setName( name );
        newRecipe.setPrice( price );
        newRecipe.setAmtCoffee( amtCoffee );
        newRecipe.setAmtMilk( amtMilk );
        newRecipe.setAmtSugar( amtSugar );
        newRecipe.setAmtChocolate( amtChocolate );
        boolean recipeEdited = coffeeMaker.editRecipe( oldRecipe, newRecipe );
        if (recipeEdited) {
            System.out.println( oldRecipe.getName() + " successfully edited." );
        } else {
            System.out.println( oldRecipe.getName() + "could not be edited." );
        }
        mainMenu();
    }

    public static void addInventory()
    {
        java.lang.String coffeeString = inputOutput( "\nPlease enter the units of coffee to add: " );
        int amtCoffee = stringToInt( coffeeString );
        if (amtCoffee < 0) {
            mainMenu();
        }
        java.lang.String milkString = inputOutput( "\nPlease enter the units of milk to add: " );
        int amtMilk = stringToInt( milkString );
        if (amtMilk < 0) {
            mainMenu();
        }
        java.lang.String sugarString = inputOutput( "\nPlease enter the units of sugar to add: " );
        int amtSugar = stringToInt( sugarString );
        if (amtSugar < 0) {
            mainMenu();
        }
        java.lang.String chocolateString = inputOutput( "\nPlease enter the units of chocolate to add: " );
        int amtChocolate = stringToInt( chocolateString );
        if (amtChocolate < 0) {
            mainMenu();
        }
        coffeeMaker.addInventory( amtCoffee, amtMilk, amtSugar, amtChocolate );
        mainMenu();
    }

    public static void checkInventory()
    {
        edu.towson.cosc603.coffeemaker.Inventory inventory = coffeeMaker.checkInventory();
        System.out.println( inventory.toString() );
        mainMenu();
    }

    public static void makeCoffee()
    {
        edu.towson.cosc603.coffeemaker.Recipe[] recipes = coffeeMaker.getRecipes();
        for (int i = 0; i < recipes.length; i++) {
            System.out.println( i + 1 + ". " + recipes[i].getName() );
        }
        java.lang.String recipeToPurchaseString = inputOutput( "Please select the number of the recipe to purchase." );
        int recipeToPurchase = stringToInt( recipeToPurchaseString ) - 1;
        if (recipeToPurchase < 0) {
            mainMenu();
        }
        java.lang.String amountPaid = inputOutput( "Please enter the amount you wish to pay" );
        int amountToPay = stringToInt( amountPaid );
        if (amountToPay < 0) {
            mainMenu();
        }
        edu.towson.cosc603.coffeemaker.Recipe recipe = recipes[recipeToPurchase];
        int change = coffeeMaker.makeCoffee( recipe, amountToPay );
        System.out.println( "Your change is: " + change + "\n" );
        mainMenu();
    }

    public static java.lang.String inputOutput( java.lang.String message )
    {
        System.out.println( message );
        java.io.BufferedReader br = new java.io.BufferedReader( new java.io.InputStreamReader( System.in ) );
        java.lang.String returnString = "";
        try {
            returnString = br.readLine();
        } catch ( java.io.IOException e ) {
            System.out.println( "Error reading in value" );
            mainMenu();
        }
        return returnString;
    }

    private static int stringToInt( java.lang.String value )
    {
        int returnInt = -1;
        try {
            returnInt = Integer.parseInt( value );
        } catch ( java.lang.NumberFormatException e ) {
            System.out.println( "Please input an integer\n" );
        }
        return returnInt;
    }

    public static void main( java.lang.String[] args )
    {
        coffeeMaker = new edu.towson.cosc603.coffeemaker.CoffeeMaker();
        System.out.println( "Welcome to the CoffeeMaker!\n" );
        mainMenu();
    }

}
